// Clientside

SCRW, SCRH = SCRW or ScrW(), SCRH or ScrH()

HG.CLIENT = {inv={}}
HG.CLIENTS = {
	Avoid = {},
	Block = {},
	Friends = {},
	Requests = {IN = {}, OUT = {}}
}

CONV_FX_DRAWHATS = CreateClientConVar( "hg_drawhats", 1, true, true )
CONV_VGUI_INFOURL = CreateClientConVar( "hg_vgui_infourl", "http://hellsgamers.com/messageoftheday/gmodrp", true, true )
CONV_VGUI_NOTICEMSG = CreateClientConVar( "hg_vgui_noticemsg", "Welcome to the new Hell's Gamers Menu for Gmod. There's new Items in the god damn shop!", true, true )

CONV_DEBUG = CreateClientConVar("hg_debug", "fuck you", true, true )

CONV_HE_PITCH 	= CreateClientConVar( "hg_hatedit_ang_p", 0, false, true )
CONV_HE_YAW 	= CreateClientConVar( "hg_hatedit_ang_y", 0, false, true )
CONV_HE_ROLL 	= CreateClientConVar( "hg_hatedit_ang_r", 0, false, true )
CONV_HE_X 		= CreateClientConVar( "hg_hatedit_pos_x", 0, false, true )
CONV_HE_Y 		= CreateClientConVar( "hg_hatedit_pos_y", 0, false, true )
CONV_HE_Z 		= CreateClientConVar( "hg_hatedit_pos_z", 0, false, true )
CONV_HE_SX 		= CreateClientConVar( "hg_hatedit_sca_x", 0, false, true )
CONV_HE_SY 		= CreateClientConVar( "hg_hatedit_sca_y", 0, false, true )
CONV_HE_SZ 		= CreateClientConVar( "hg_hatedit_sca_z", 0, false, true )

CONV_VGUI_HAT = CreateClientConVar( "hg_hat", "", true, true )
cvars.AddChangeCallback( "hg_hat", function( cvar, last, new )
	RunConsoleCommand("hg_item_cmd", "equip", new)
	LocalPlayer():SetPData("HG_HAT", new)
end)
CONV_VGUI_TRAIL = CreateClientConVar( "hg_trail", "", true, true )
cvars.AddChangeCallback( "hg_trail", function( cvar, last, new )
	RunConsoleCommand("hg_item_cmd", "equip", new)
	LocalPlayer():SetPData("HG_TRAIL", new)
end)
CONV_VGUI_AURA = CreateClientConVar( "hg_aura", "", true, true )
cvars.AddChangeCallback( "hg_aura", function( cvar, last, new )
	RunConsoleCommand("hg_item_cmd", "equip", new)
	LocalPlayer():SetPData("HG_AURA", new)
end)

function HG.GetTextSize(s_Font,s_String)
	surface.SetFont(s_Font)
	local w, h = surface.GetTextSize(tostring(s_String))
	return w, h
end

function HG.GetClientEquipment()
	local hat, trail, aura = GetConVarString("hg_hat"), GetConVarString("hg_trail"), GetConVarString("hg_aura")
	print( "Hat: "..tostring(hat).."\tTrail: "..tostring(trail).."\tAura: "..tostring(aura) )
	return { hat, trail, aura }
end

function HG.IsClientItemEquiped(itemid)
	return table.HasValue(HG.GetClientEquipment(),itemid)
end

function HG.ResetEquipment( )
	--LocalPlayer():hg_CreateEquipmentDummy()
	RunConsoleCommand( "hg_hat", "" )
	RunConsoleCommand( "hg_trail", "" )
	RunConsoleCommand( "hg_aura", "" )
end
timer.Simple(0.1,function() HG.ResetEquipment() end)

// Keep an eye on the screen resolution incase it changes
hook.Add( "Tick", "hgf_UpdateSRes", function()
	if SCRW == ScrW() && SCRH == ScrH() then 
		return
	else
		if SCRW != ScrW() then SCRW = ScrW() end
		if SCRH != ScrH() then SCRH = ScrH() end
	end
end)

local b_DoOnce = false
hook.Add( "PlayerBindPress", "hgf_OpenMenu", function( pl, bind, pressed )
	if pl:KeyDown(IN_WALK) and bind == "gm_showhelp" and not b_DoOnce then
		RunConsoleCommand("hg_menu2")
		b_DoOnce = true
		timer.Simple(1,function()b_DoOnce = false end)
		return true
	end
end )

usermessage.Hook( "HGF_InitData", function(um)
	HG.CLIENT = { hgid = 0, steamid = "", uniqueid = 0, money = 0, inv = {} }
end)

usermessage.Hook( "HGF_UpdateData", function(um)
	HG.CLIENT = { hgid = um:ReadShort(), steamid = um:ReadString(), uniqueid = um:ReadShort(), money = um:ReadShort(), inv = {} }
end)

usermessage.Hook( "HGF_UpdateItems", function(um)
	print( "Updating items from the server| ItemString = " )
	local count = um:ReadShort()
	local items = um:ReadString()
	items = string.Explode("|",items)

	for k, id in ipairs(items) do
		
		print( k, id )
		
		if id == "" or id == " " then return end
		
		for j, v in pairs(HG.OrganizedItems()) do
		
			--print( "_"..id.."_", j, v )
			if tostring(j) == id then
				print( "Found item UniqueID "..tostring(v))
				table.insert( HG.CLIENT.inv, k, v )
			end
			
		end


	end
end)

usermessage.Hook( "HGF_ModMoney", function(um)
	HG.CLIENT.money = um:ReadLong()
end)

usermessage.Hook( "HGF_AddItem", function(um)
	table.insert( HG.CLIENT.inv, um:ReadString() )
end)
